﻿/*----------register----------*/
/*email*/
function checkEmail() {
    var result = true;
    var email = $("#email");
    if (email.val() == "") {
        $("#emailText").html('<span style="color:#ff0000;">邮箱不能为空.</span>');
        result = false;
    }
    else {
        var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        if (!reg.test(email.val())) {
            $("#emailText").html('<span style="color:#ff0000;">邮箱格式错误.</span>');
            result = false;
        }
    }
    return result;
}
function resetEmail() {
    $("#emailText").html('');
}
/*username*/
function checkUsername() {
    var result = true;
    var username = $("#username");
    if (username.val() == "") {
        $("#usernameText").html('<span style="color:#ff0000;">用户名不能为空.</span>');
        result = false;
    }
    else {
        var reg = /^[a-zA-Z\d]\w{3,15}[a-zA-Z\d]$/;
        if (!reg.test(username.val())) {
            $("#usernameText").html('<span style="color:#ff0000;">用户名由字母、数字组成，4-16位.</span>');
            result = false;
        }
    }
    return result;
}
function resetUsername() {
    $("#usernameText").html('');
}
/*password*/
function checkPassword() {
    var result = true;
    var password = $("#password");
    if (password.val() == "") {
        $("#passwordText").html('<span style="color:#ff0000;">密码不能为空.</span>');
        result = false;
    }
    else {
        var reg = /^[a-zA-Z\d]\w{5,15}[a-zA-Z\d]$/;
        if (!reg.test(password.val())) {
            $("#passwordText").html('<span style="color:#ff0000;">密码由字母、数字组成，6-16位.</span>');
            result = false;
        }
    }
    if ($("#password2").val() != "" && password.val() != "") {
        if ($("#password2").val() != password.val()) {
            $("#passwordText").html('<span style="color:#ff0000;">两次输入的密码不相同.</span>');
            result = false;
        }
    }
    return result;
}
function resetPassword() {
    $("#passwordText").html('');
}
/*password2*/
function checkPassword2() {
    var result = true;
    var password2 = $("#password2");
    if (password2.val() == "") {
        $("#passwordText2").html('<span style="color:#ff0000;">确认密码不能为空.</span>');
        result = false;
    }
    else {
        var reg = /^[a-zA-Z\d]\w{5,15}[a-zA-Z\d]$/;
        if (!reg.test(password2.val())) {
            $("#passwordText2").html('<span style="color:#ff0000;">密码由字母、数字组成，6-16位.</span>');
            result = false;
        }
    }
    if ($("#password").val() != "" && password2.val() != "") {
        if ($("#password").val() != password2.val()) {
            $("#passwordText2").html('<span style="color:#ff0000;">两次输入的密码不相同.</span>');
            result = false;
        }
    }
    return result;
}
function resetPassword2() {
    $("#passwordText2").html('');
}
/*phone*/
function checkPhone() {
    var result = true;
    var phone = $("#telphone");
    if (phone.val() != "") {

        var reg = /^0?1[3|4|5|8][0-9]\d{8}$/;
        if (!reg.test(phone.val())) {
            $("#telphoneText").html('<span style="color:#ff0000;">只能填数字.</span>');
            result = false;
        }
        if (phone.val().length != 11) {
            $("#telphoneText").html('<span style="color:#ff0000;">手机号为11位.</span>');
            result = false;
        }
    }
    return result;
}
function resetPhone() {
    $("#telphoneText").html('');
}
/*submit*/
function register() {
    var isreg = false;
    var a = checkEmail();
    var b = checkUsername();
    var c = checkPassword();
    var d = checkPassword2();
    var e = checkPhone();
    if (a && b && c && d && e) {
        isreg = true;
    }
    if (isreg) {
        $.ajax({
            url: 'account/addUser',
            data: $("#user").serialize(),
            type: 'POST',
            success: function (msg) {
                if (msg == "success") {
                    alert("注册成功，请登录");
                }
                else {
                    alert(msg);
                }
            },
            error: function (msg) {
                alert("网络异常，添加失败");
            }
        });
    }
}